import { ActionContext, ConditionContext } from './context';
import { UserPlayer } from './player';

export type ActionCallback = (context: ActionContext) => Promise<void>;

export type NextNode =
	| {
			nodeId: string;
			players: UserPlayer[];
	  }
	| string;

export interface FlowActionInfo {
	readonly id: string;
	readonly name: string;
	readonly choosePlayer: boolean;
	readonly nextNodes: NextNode[];
	readonly condition: { desc: string } | null;
}

export class FlowAction implements FlowActionInfo {
	condition: {
		desc: string;
		match: (context: ConditionContext) => Promise<boolean>;
	} | null = null;
	constructor(
		public id: string,
		public name: string,
		public nextNodes: string[],
		public choosePlayer: boolean = true,
		public onAction?: ActionCallback
	) {}
	static toActionInfo(action: FlowAction): FlowActionInfo {
		return {
			id: action.id,
			name: action.name,
			choosePlayer: action.choosePlayer,
			nextNodes: action.nextNodes.slice(),
			condition: action.condition ? { desc: action.condition?.desc } : null,
		};
	}
}
